import { Ast, Visitor } from "./ast";

export class BaseVisitor<C = any, T = any> implements Visitor<C, T>{
    visitNode(node: Ast, ctx: C): T {
        if(!node.visibleToUser) return;
        if (node.className) {
            const classNameSplit = node.className.split('.')
            const classNameEnd = classNameSplit.pop()
            const methodName = `visit${classNameEnd.charAt(0).toUpperCase() + classNameEnd.slice(1)}`
            if (this[methodName]) return this[methodName](node, ctx)
        }
        node.children.map(c => this.visitNode(c, ctx))
    }
}

export class CoreVisitor<C = any, T = any> implements Visitor<C, T>{
    visitNode(node: Ast, ctx: C): T {
        if (node.className) {
            const classNameSplit = node.className.split('.')
            const classNameEnd = classNameSplit.pop()
            const methodName = `visit${classNameEnd.charAt(0).toUpperCase() + classNameEnd.slice(1)}`
            if (this[methodName]) return this[methodName](node, ctx)
        }
        node.children.map(c => this.visitNode(c, ctx))
    }
}